class UsersController < ApplicationController

  def new
  end

  def create
    @user = User.create(params[:user])
    @user.set_activation_key
    @user.save!

    begin
      UserMailer.deliver_signup(@user, request.host_with_port)
    rescue => e
      flash[:notice] = "An error occured while sending the signup message to #{@user.email}. Please check the e-mail address."
      redirect_to :action => "new"
    end

    redirect_to :controller => 'home', :action => 'index'
    flash[:notice] = "An activation email has been sent to #{@user.email}."

  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end

  def activate
    user = User.find_by_activation_key(params[:key])
    if user.not_activated?
      user.activate!
      flash[:notice] = "Signup complete!"
    end
    redirect_to :controller => 'session', :action => 'new'
  end

end
